"""
Copyright 2012 Wieser Dietmar, Margit Mutschlechner.

This file is part of Multimedia Social Networking Platform.

Multimedia Social Networking Platform is free software: 
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Multimedia Social Networking Platform is distributed
in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Multimedia Social Networking Platform.  
If not, see <http://www.gnu.org/licenses/>.

"""

import traceback
from msnp.service import utils

########
#tables#
########

TABLE_LOG = "log"


#########################
#columns for log table#
#########################
COLUMN_ID = "id"
COLUMN_DESCRIPTION = "description"
COLUMN_TIMESTAMP = "timestamp"


def dropTable(database, cursor, schema):
    """Cleans the whole database.
    
    Deletes all entries of all tables.
    Returns True if deletion was successful, or False if an error occurred."""
    try:
        query = "drop table if exists " + schema + "." + TABLE_LOG
        cursor.execute(query)
        database.commit()
    except:
        database.rollback()
        
def executeQuery(database, cursor, query):
    """Executes the given query on the given database.
    
    Args:
        database: A Connection object representing the database where the query should be executed.
        cursor: A Cursor object needed for the execution of the database query.
        query: A String containing the query to be executed.
    """
    try:
        cursor.execute(query)
        database.commit()
    except:
        traceback.print_exc()
        
def log(database, cursor, schema, description):
    """Writes the given String into the log table of the ptp database.
    
    Args:
        database: The database where the log table is located.
        cursor: A cursor needed for the insertion of the new log entry.
        schema: The name of the schema where the log table is located.
        description: A String containing the log message.
    """
    try:
        query = __getQuery(schema, description)
        cursor.execute(query)
        database.commit()
    except:
        database.rollback()
    
    
def __getQuery(schema, description):
    """Constructs a query for inserting the given String into the log table of the ptp database.
    
    Args:
        schema: A String containing the name of the schema where the log table is located.
        description: A String containing the message to be inserted.
    """
    timestamp = utils.DateFormatter.formatTimeWithDay()
    query = "insert into " + schema + "." + TABLE_LOG + " (" + COLUMN_DESCRIPTION + ", " + COLUMN_TIMESTAMP + ") values ('" + description + "', '" + timestamp + "')"
    return query

    
